package com.joinway.framework.extension.groovy

import java.lang.reflect.Method

import org.apache.commons.lang3.StringUtils

import com.joinway.framework.bean.domain.constant.FrameworkProperty
import com.joinway.framework.bean.domain.constant.SqlConstants

public final class DBConverter {

	private static final String MYSQL_ESCAPE_CHAR = "`"
	private static final String HSQLDB_ESCAPE_CHAR = ''
	
//	public static String escapeColumnName(String name){
//		return escapeColumnName(name, null)
//	}
	
	public static String escapeColumnName(String name, String dbType = null){
		String qname = name
		
		boolean match = false
		if(qname ==~ /\s*\*\s*/ 	// select *
			|| qname ==~ /(?i)\s*count\s*\(\s*[a-zA-Z_0-9*]+\s*\)\s*/	// select count(x)
			|| qname ==~ /(?i)\s*sum\s*\(\s*[a-zA-Z_0-9*]+\s*\)\s*/)	// select sum(x)
		{
			match = true
		}
		
		String escapeStr = getDbEscapeString(dbType)
		
		if(qname && !match && escapeStr){
			if(!qname.startsWith(escapeStr)){
				qname = escapeStr + qname
			}
			if(!qname.endsWith(escapeStr)){
				qname += escapeStr
			}
		}
		
		qname
	}
	
//	public static String unescapeColumnName(String name){
//		return unescapeColumnName(name, null)
//	}
	
	public static String unescapeColumnName(String name, String dbType = null){
		String escapeStr = getDbEscapeString(dbType)
		
		String uname = name
		
		if(uname && escapeStr){
			if(uname.startsWith(escapeStr)){
				uname = uname.substring(1, uname.length())
			}
			if(uname.endsWith(escapeStr)){
				uname = uname.substring(0, uname.length() - 1)
			}
		}
		
		uname
	}
	
//	private static String convertColumnName(String name, String methodType, String dbType = SqlConstants.DataBase.MySql ){
//		String type = dbType ? dbType : getDbType()
//		
//		String methodName = methodType + StringUtils.capitalize(type)
//		
//		Method method = DBConverter.class.getDeclaredMethods().find {it.getName().equals(methodName)}
//		
//		String ename = method ? DBConverter."$methodName"(name) : name
//		return ename
//	}
	
//	private static String escapeColumnNameForMysql(String name){
//		String qname = name
//		
//		boolean match = false
//		if(qname ==~ /\s*\*\s*/ 	// select *
//			|| qname ==~ /(?i)\s*count\s*\(\s*[a-zA-Z_0-9*]+\s*\)\s*/	// select count(x)
//			|| qname ==~ /(?i)\s*sum\s*\(\s*[a-zA-Z_0-9*]+\s*\)\s*/)	// select sum(x)
//		{
//			match = true
//		}
//		
//		if(qname && !match){
//			if(!qname.startsWith(MYSQL_ESCAPE_CHAR)){
//				qname = "`" + qname
//			}
//			if(!qname.endsWith(MYSQL_ESCAPE_CHAR)){
//				qname += "`"
//			}
//		}
//		
//		qname
//	}
	
//	private static String unescapeColumnForMysql(String name){
//		String uname = name
//		
//		if(uname){
//			if(uname.startsWith(MYSQL_ESCAPE_CHAR)){
//				uname = uname.substring(1, uname.length())
//			}
//			if(uname.endsWith(MYSQL_ESCAPE_CHAR)){
//				uname = uname.substring(0, uname.length() - 1)
//			}
//		}
//		
//		uname
//	}

	private static String getDbType(){
		String type = System.getProperty(FrameworkProperty.DB_TYPE)
		if(!type){
			type = SqlConstants.DataBase.MySql
		}
		type
	}
	
	private static String getDbEscapeString(String dbType = null){
		String str = ""
		String type = dbType ? dbType : getDbType()
		
		if(SqlConstants.DataBase.MySql.equals(type)){
			str = MYSQL_ESCAPE_CHAR
		}else if(SqlConstants.DataBase.Hsqldb.equals(type)){
			str = HSQLDB_ESCAPE_CHAR
		}
		
		str
	}
	
}
